<template>
  <div class="com-container">
    <div class="com-chart" id="stock"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data () {
    return {
      chartId: null,
      allData: null,
      activeIndex: 0,
      timerId: null,
      titleFontSize: 0
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    // 监听主题的变化  主题变化后更新图表
    theme () {
      this.chartId.dispose() // 重新渲染组件
      this.initChart()
      this.screenAdapter()
      this.updateChart()
    }
  },
  methods: {
    initChart () {
      this.chartId = this.$echarts.init(
        document.getElementById('stock'),
        this.theme
      )
      const initOption = {
        title: { text: '▎ 库存销售量', left: 20, top: 20 }
      }
      this.chartId.setOption(initOption)
      // 鼠标移入销毁定时器
      this.chartId.on('mouseover', () => {
        clearInterval(this.timerId)
      })
      // 鼠标移出开始定时器
      this.chartId.on('mouseout', () => {
        this.setInterTimer()
      })
    },
    async getData () {
      const res = await this.$http.get('stock')
      this.allData = res.data
      this.updateChart()
      this.setInterTimer()
    },
    updateChart () {
      // 显示的圆环的位置
      const centerPointers = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%']
      ]
      // 显示颜色的数组
      const colorArrs = [
        ['#4FF778', '#0BA82C'],
        ['#E5DD45', '#E8B11C'],
        ['#E8821C', '#E55445'],
        ['#5052EE', '#AB6EE5'],
        ['#23E5E5', '#2E72BF']
      ]
      // 显示的圆环的数据
      const start = this.activeIndex * 5
      const end = (this.activeIndex + 1) * 5
      const showData = this.allData.slice(start, end)
      const seriesArr = showData.map((item, index) => {
        return {
          label: { show: true, position: 'center', color: colorArrs[index][0] }, // 关闭提示
          labelLine: { show: false }, // 提示线
          hoverAnimation: false, // 关闭动画
          type: 'pie', // 图表类型
          center: centerPointers[index], // 图表位置
          data: [
            {
              name: item.name + '\n\n' + item.sales,
              value: item.sales,
              // 开启颜色渐变
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  { offset: 0, color: colorArrs[index][0] },
                  { offset: 1, color: colorArrs[index][1] }
                ])
              }
            },
            {
              value: item.stock,
              itemStyle: { color: '#333843' }
            }
          ] // 图表数据
        }
      })
      const dataOption = {
        series: seriesArr
      }
      this.chartId.setOption(dataOption)
    },
    screenAdapter () {
      this.titleFontSize = (document.getElementById('stock').offsetWidth / 100) * 3.6
      const innerRadius = this.titleFontSize * 2.8
      const outterRadius = innerRadius * 1.125
      const resizeOption = {
        title: { textStyle: { fontSize: this.titleFontSize } },
        series: [
          { type: 'pie', radius: [outterRadius, innerRadius], label: { fontSize: this.titleFontSize / 2 } },
          { type: 'pie', radius: [outterRadius, innerRadius], label: { fontSize: this.titleFontSize / 2 } },
          { type: 'pie', radius: [outterRadius, innerRadius], label: { fontSize: this.titleFontSize / 2 } },
          { type: 'pie', radius: [outterRadius, innerRadius], label: { fontSize: this.titleFontSize / 2 } },
          { type: 'pie', radius: [outterRadius, innerRadius], label: { fontSize: this.titleFontSize / 2 } },
          { type: 'pie', radius: [outterRadius, innerRadius], label: { fontSize: this.titleFontSize / 2 } }
        ]
      }
      this.chartId.setOption(resizeOption)
      this.chartId.resize()
    },
    setInterTimer () {
      if (this.timerId) {
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(() => {
        this.activeIndex++
        if (this.activeIndex > 1) {
          this.activeIndex = 0
        }
        this.updateChart()
      }, 2000)
    }
  },
  mounted () {
    this.initChart()
    this.getData()
    this.screenAdapter()
    window.addEventListener('resize', this.screenAdapter)
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timerId) // 组件销毁时清空定时器
  }
}
</script>

<style>
</style>
